/**
 * @author silence
 * @class app
 * @create_date 2016-05-02
 * @update_date 
 * @description 注册
 */
register_page = {
    initEvent: function(e, id, page) {
        if (app.getItem('openid') != null && app.getItem('Lideta') != null && app.getLoginUser() != null) {
            var newday = new Date().getTime();
            var oldday = parseInt(app.getItem('Lideta'));
            var days = newday - oldday;
            var howday = Math.floor(days / (24 * 3600000));
           // var howday = Math.floor(days /1800000);
           var openid=app.getParameterByName('openid');
           var oldopenid=app.getItem('openid');
            if (howday<1) {
                //判断openid是否一致
                if(openid==oldopenid){
                     app.go('index.html');
                }else{
                    register_page.register(page);
                }
            } else {
                register_page.register(page);

            }
        } else {
            register_page.register(page);
        }
    },
    register: function(page) {
        $('#page_register').show();
        //清除手机号
        $(document).on('click','.rclose',function(){
            $('#iphone').val('');
        })
        //获取验证码
        $(document).on('click', '.RYanz', function() {
            $('.yzm').removeClass('RYanz');
            var filter = /^1\d{10}$/;
            var iphone = $('#iphone').val();
            if (iphone == "") {
                $.alert("手机号不能为空");
                $('.yzm').addClass('RYanz');
                return false;
            }
            if (filter.test(iphone) == false) {
                $.alert("手机号错误");
                $('.yzm').addClass('RYanz');
                return false;
            } else {
                var r = JSON.stringify({ 'phone': iphone });
                app.ajax(app.url.api_base + 'conCtl/con/getCheckNum?data=' + r, {
                }, function(d) {
                    if (d.result != 10000) {
                        $.alert(d.msg);
                        $('.yzm').addClass('RYanz');
                    } else {
                            //判断是否有数据
                        if (d.data != null) {
                            var d = JSON.parse(d.data.split(':')[1].trim());
                            // $.alert(d)
                            $('#page_register').attr('unm', d);
                        } else {
                            $.alert(d.msg)
                        }
                        var num = 60
                        clearInterval(timer);
                        var timer = setInterval(function() {
                            num = num - 1;
                            $('.yzm').html('重新获取(' + num + ')');
                            if (num == 0) {
                                clearInterval(timer);
                                $('.yzm').addClass('RYanz');
                                $('.yzm').html("获取验证码");
                            }
                        }, 1000)

                    }
                }, function() {}, { load: false, title: '加载中...' })
            }
        })

        //注册登录
        $(document).on('click', '.denglu a', function() {
            var filter = /^1\d{10}$/;
            var iphone = $('#iphone').val();
            var checkNum = $('#yanzhengma').val().trim()
            if (iphone == "") {
                $.alert("手机号不能为空")
                return false;
            }
            if (filter.test(iphone) == false) {
                $.alert("手机号错误");
                return false;
            }
            if ($('#yanzhengma').val() == '') {
                $.alert('验证码不能为空！');
                return false;
            } else if (checkNum != $('#page_register').attr('unm')) {
                $.alert('验证码错误!');
                return false;
            }
            var r = JSON.stringify({
                'phone': iphone,
                'checkNum': checkNum
            })
            app.ajax(app.url.api_base + 'conCtl/con/checkNumLogin?data=' + r, {}, function(d) {
                if (d.result != 10000) {
                    $.alert(d.msg);
                } else {
                    app.addItem('openid', app.getParameterByName('openid'));
                    app.addItem('userPhone', iphone);
                    app.putLoginUser(d.data.studentId);
                    app.addItem('Lideta', new Date().getTime());
                    app.go('index.html');
                }
            }, function() {}, { load: true, title: '加载中...' })
        })

    }
}
$(function() {
    $(document).on("pageInit", "#page_register", function(e, id, page) {
        register_page.initEvent(e, id, page);
    });
    $.init();
})
